package com.zhuss.product.processor;

import com.zhuss.bean.bo.product.train.BindStrategySiteBo;
import com.zhuss.bean.bo.product.train.CreateStrategySiteBo;
import com.zhuss.bean.vo.product.line.DetailLineVo;
import com.zhuss.bean.vo.product.train.DetailLineStrategyVo;
import com.zhuss.bean.vo.product.train.ListLineStrategySiteVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.enums.OperationStatus;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.LineDao;
import com.zhuss.product.db.dao.ext.LineStrategyDao;
import com.zhuss.product.db.dao.ext.LineStrategySiteDao;
import com.zhuss.product.db.dao.ext.TrainDao;
import com.zhuss.product.db.entity.Line;
import com.zhuss.product.db.entity.LineStrategy;
import com.zhuss.product.db.entity.LineStrategySite;
import com.zhuss.product.processor.cache.LineCache;
import com.zhuss.product.processor.cache.SiteCache;
import com.zhuss.product.processor.cache.SiteStrategyCache;
import com.zhuss.product.processor.cache.StrategySiteCache;
import com.zhuss.util.DateUtil;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author summer
 * @date 2021/1/13 9:59
 */

@Component("strategySiteBind")
public class StrategySiteBind extends AbstractProcessor<Object, BindStrategySiteBo> implements Processor<Object, BindStrategySiteBo> {

    @Autowired
    private LineStrategySiteDao lineStrategySiteDao;

    @Autowired
    @Qualifier("strategySiteCache")
    private StrategySiteCache strategySiteCache;

    @Autowired
    @Qualifier("siteStrategyCache")
    private SiteStrategyCache siteStrategyCache;

    @Autowired
    private TrainDao trainDao;

    @Autowired
    private LineStrategyDao lineStrategyDao;

    @Autowired
    @Qualifier("lineCache")
    private LineCache lineCache;

    @Autowired
    @Qualifier("siteCache")
    private SiteCache siteCache;

    @Override
    public void check(BindStrategySiteBo bo) throws DzcxException {
        if (bo.getLineStrategyId() == null || bo.getLineStrategyId() <= 0l) {
            throw new DzcxException(ExceptionCode.策略ID为空);
        }

        if (bo.getStrategySiteVoList().size() >0 && bo.getStrategySiteVoList() !=null) {
            DetailLineStrategyVo strategyVo = lineStrategyDao.detail(bo.getLineStrategyId());

            DetailLineVo lineVo = lineCache.get(strategyVo.getLineId());
            String start = siteCache.get(bo.getStrategySiteVoList().get(0).getSiteId()).getName();
            if (!lineVo.getStartPointName().equals(start)) {
                throw new DzcxException(ExceptionCode.首站名称和线路起点名称不一致);
            }
            if (bo.getStrategySiteVoList().size() >1) {
                String end = siteCache.get(bo.getStrategySiteVoList().get(bo.getStrategySiteVoList().size() -1).
                        getSiteId()).getName();
                if (!lineVo.getEndPointName().equals(end)) {
                    throw new DzcxException(ExceptionCode.末站名称和线路终点名称不一致);
                }
            }
        }
        
        Date now = DateUtil.toDateOnly(new Date());
        if (trainDao.count(bo.getLineStrategyId(), OperationStatus.已开通.key(), now) >0) {
            throw new DzcxException(ExceptionCode.当前班次正在运营中);
        }
    }

    @Override
    public Object process(BindStrategySiteBo bo) throws DzcxException {

        //先清除缓存
        List<ListLineStrategySiteVo> strategySiteVoList = strategySiteCache.get(bo.getLineStrategyId());
        strategySiteVoList.forEach(site ->{
            siteStrategyCache.clear(site.getSiteId());
        });
        siteCache.clear();
        strategySiteCache.clear(bo.getLineStrategyId());


        //根据lineStrategyId删除站点
        LineStrategySite strategySite = new LineStrategySite();
        strategySite.setLineStrategyId(bo.getLineStrategyId());
        strategySite.setModifierId(bo.getConcurrentUserId());
        strategySite.setModifier(bo.getConcurrentUserName());
        strategySite.setModifyTime(new Date());
        strategySite.setDeleted(bo.getDeleted());
        lineStrategySiteDao.delete(strategySite);
        //插入 LineStrategySite
        for (CreateStrategySiteBo vo :bo.getStrategySiteVoList()) {
            if (vo.getPrice() == null ) {
                throw new DzcxException(ExceptionCode.票价权重为空);
            }
            if (vo.getNearestDuration() == null ) {
                throw new DzcxException(ExceptionCode.间隔时间为空);
            }
            StrategySiteSave(vo, bo.getLineStrategyId());
        }
        return null;
    }

    public void StrategySiteSave(CreateStrategySiteBo param, Long lineStrategyId) {
        LineStrategySite record = new LineStrategySite();
        BeanUtils.copyProperties(param, record);
        record.setLineStrategyId(lineStrategyId);
        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        lineStrategySiteDao.create(record);

    }

    @Override
    public Object log(BindStrategySiteBo bo) throws DzcxException {
        return null;
    }
}
